// 1.定义一个接口
interface IMenu {
    name: string;
    show(): void;
}

// 2.定义一个组合器类
class Menu implements IMenu {
    name: string;
    children: IMenu[];

    constructor(name: string) {
        this.name = name;
        this.children = [];
    }

    add(menu: IMenu) {
        this.children.push(menu);
    }

    remove(menu: IMenu) {
        const index = this.children.findIndex((item) => item === menu);
        this.children.splice(index, 1);
    }

    show() {
        console.log(this.name);
        this.children.forEach((item) => item.show());
    }
}

// 3.实例化组合器类
const menu1 = new Menu('菜单1');
const menu2 = new Menu('菜单2');
const menu3 = new Menu('菜单3');
menu1.add(menu2);
menu2.add(menu3);
menu1.show(); // 菜单1 菜单2 菜单3